package io.plus.common.protocol;

import io.plus.common.packet.metadata.Command;
import io.plus.engine.pulse.AsynchronousSocketChannelContext;
import io.plus.server.SessionContext;
import io.plus.server.Status;


public class PacketConverter implements IPacketConverter {

    @Override
    public Packet converteRequestPacket(AsynchronousSocketChannelContext asynchronousSocketChannelContext, Command command, byte[] body) {
        Object sessionContext = asynchronousSocketChannelContext.getAttribute();
        if (sessionContext instanceof SessionContext) {//转TCP协议请求包;
            Packet packet = new Packet(command, Status.ok, body);
            Encoder.encode(asynchronousSocketChannelContext.getClientConfiguration(), asynchronousSocketChannelContext, packet);
            packet.setCommand(command);
            return packet;
        }
        return null;
    }

    @Override
    public Packet converteResponsepPacket(AsynchronousSocketChannelContext asynchronousSocketChannelContext, Command command, byte[] body) {
        Object sessionContext = asynchronousSocketChannelContext.getAttribute();
        if (sessionContext instanceof SessionContext) {//转TCP协议响应包;
            Packet packet = new Packet(command, Status.ok, body);
            Encoder.encode(asynchronousSocketChannelContext.getClientConfiguration(), asynchronousSocketChannelContext, packet);
            packet.setCommand(command);
            return packet;
        }
        return null;
    }

}
